import Storage from 'store'

let record = null
const NAMESPACE = 'V_CRUD'

export function useTableSettingRecord(name) {
  const key = name ? `${NAMESPACE}__${name}` : ''

  if (record === null) {
    record = Storage.get(key) || {}
  }

  const setRecord = (k, obj) => {
    if (!key || !k) return

    Object.keys(obj).forEach(k => {
      if (k === '' || k == 'undefined') {
        delete obj[k]
      }
    })

    record[k] = obj
    Storage.set(key, record)
  }

  return {
    setRecord,
    widthRecord: record.width || {},
    isShowRecord: record.isShow || {},
    sortRecord: record.sort || {}
  }
}